import tkinter.messagebox
from tkinter import *

calculator = Tk()
remove = Label(calculator, text='123456789')
calculator.geometry("300x170+500+200")
title = Label(calculator, text='20202418于宗源的计算器')  # 标题
num1_label = Label(calculator, text='输入数字1')  # 提示信息1
title.grid(row=0, column=1)  # 把标题放在第0行，第一列的位置
title1 = Label(calculator, text='注意 j 代表虚数！')
title1.grid(row=0, column=0)
num1_label.grid(row=1, column=0, padx=10)  # 放在第二行，第0列的位置
num1 = Entry(calculator)
num1.grid(row=1, column=1)
num2_label = Label(calculator, text='输入数字2')
num2_label.grid(row=2, column=0, padx=20)
num2 = Entry(calculator)
num2.grid(row=2, column=1, padx=20)
result_var = StringVar()
result_var.set("结果为")

def error_message():
    tkinter.messagebox.showinfo(title="出错啦我滴宝！", message="检查检查你的输入吧~")


def Divby0():
    tkinter.messagebox.showinfo(title="出错啦我滴宝！", message="是不是除0了呢")


def add():
    try:
        a = float(num1.get())
        b = float(num2.get())
        result_var.set('结果是{}'.format(a + b))
    except:
        try:
            a = complex(num1.get())
            b = complex(num2.get())
            result_var.set('结果是{}'.format(a + b))
        except:
            error_message()


add_ = Button(calculator, text='+', width=5, command=add)
add_.grid(row=3, column=0, padx=30)


def subtraction():
    try:
        a = float(num1.get())
        b = float(num2.get())
        result_var.set('结果是{}'.format(a - b))
    except:
        try:
            a = complex(num1.get())
            b = complex(num2.get())
            result_var.set('结果是{}'.format(a - b))
        except:
            error_message()


subtraction_ = Button(calculator, text='-', width=5, command=subtraction)
subtraction_.grid(row=3, column=1, padx=30)


def multiplication():
    try:
        a = float(num1.get())
        b = float(num2.get())
        result_var.set('结果是{}'.format(a * b))
    except:
        try:
            a = complex(num1.get())
            b = complex(num2.get())
            result_var.set('结果是{}'.format(a * b))
        except:
            error_message()


multiplication_ = Button(calculator, text='x', width=5, command=multiplication)
multiplication_.grid(row=4, column=0, padx=30)


def division():
    try:
        a = float(num1.get())
        b = float(num2.get())
        result_var.set('结果是{}'.format(a / b))
    except ValueError:
        try:
            a = complex(num1.get())
            b = complex(num2.get())
            result_var.set('结果是{}'.format(a / b))
        except ZeroDivisionError:
            Divby0()
        except ValueError:
            error_message()
    except ZeroDivisionError:
        Divby0()


division_ = Button(calculator, text='/', width=5, command=division)
division_.grid(row=4, column=1, padx=30)


def mod():
    try:
        a = float(num1.get())
        b = float(num2.get())
        result_var.set('结果是{}'.format(a % b))
    except ZeroDivisionError:
        Divby0()
    except ValueError:
        error_message()


mod_ = Button(calculator, text='%', width=5, command=mod)
mod_.grid(row=5, column=0, padx=30)
result = Label(calculator, width=30, textvariable=result_var, justify="left")
result.grid(row=5, column=1)
calculator.mainloop()
